package com.tandashi.springbootstudy01.config.database;

import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@SuppressWarnings("unused")
@Configuration
@MapperScan(basePackages = "com.tandashi.springbootstudy01.dao.rrd",sqlSessionFactoryRef = "rrdSqlSessionFactory")
@Order(1)
public class OperRrdDataSourceConfig {

    private static final String mapper_location = "classpath:mybatis/mapper/rrd/*.xml";
    private static final String alias_package = "com.tandashi.springbootstudy01.domain.activity";
    private static final String mybatis_config_location = "mybatis/mybatis-config.xml";

    @Value("${rrd.datasource.url}")
    private String url;

    @Value("${rrd.datasource.username}")
    private String username;

    @Value("${rrd.datasource.password}")
    private String password;

    @Value("${rrd.datasource.driver-class-name}")
    private String driverClass;

    @Bean(name = "rrdDataSource")
    public DataSource rrdDataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setJdbcUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setPoolName("rrdDataSource");
        dataSource.setMinimumIdle(2);
        dataSource.setMaximumPoolSize(20);

        dataSource.setConnectionTimeout(6000000);
        dataSource.setMaxLifetime(1800000);
        dataSource.setIdleTimeout(600000);

        return dataSource;
    }

    /**
     * 配置事务管理器，不然没有事务
     */
    @Bean(name = "rrdTransactionManager")
    public DataSourceTransactionManager rrdTransactionManager(@Qualifier("rrdDataSource") DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "rrdSqlSessionFactory")
    public SqlSessionFactory rrdSqlSessionFactory(@Qualifier("rrdDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources(mapper_location));
        sqlSessionFactoryBean.setConfigLocation(new ClassPathResource(mybatis_config_location));

        sqlSessionFactoryBean.setVfs(SpringBootVFS.class);
        sqlSessionFactoryBean.setTypeAliasesPackage(alias_package);
        return sqlSessionFactoryBean.getObject();
    }
}
